/**
 * created by 江心才子, 2019/8/20
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.base.share

import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import com.wyl.easydialog.EasyDialog
import com.wyl.easydialog.R
import com.wyl.easydialog.ViewHolder
import com.wyl.easydialog.ViewListener
import com.wyl.libbase.extend.copyToClipboard
import com.wyl.libbase.extend.toast

/**
 * 项目名称：o2o
 * 创建人：江心才子
 * 创建时间：2019-08-20 15:51
 * 内容描述：
 * 修改说明：
 */

fun AppCompatActivity.showShareUrlDialog(
    url: String,
    title: String,
    thumb: UMImage,
    description: String,
    listener: UMShareListener
) {
    val activity = this
    EasyDialog.Builder()
        .setLayoutId(R.layout.share_dialog)
        .setGravity(Gravity.BOTTOM)
        .setViewLisenter(object : ViewListener() {
            override fun convert(helper: ViewHolder, dialog: EasyDialog) {
                helper.apply {
                    setOnClickListener(R.id.qq, View.OnClickListener {
                        share(activity, SHARE_MEDIA.QQ, url, title, thumb, description, listener)
                        dialog.dismiss()
                    })
                    setOnClickListener(R.id.wx, View.OnClickListener {
                        share(
                            activity,
                            SHARE_MEDIA.WEIXIN,
                            url,
                            title,
                            thumb,
                            description,
                            listener
                        )
                        dialog.dismiss()
                    })
                    setOnClickListener(R.id.pyq, View.OnClickListener {
                        share(
                            activity,
                            SHARE_MEDIA.WEIXIN_CIRCLE,
                            url,
                            title,
                            thumb,
                            description,
                            listener
                        )
                        dialog.dismiss()
                    })
                    setOnClickListener(R.id.link, View.OnClickListener {
                        activity.copyToClipboard(url)
                        toast("复制成功")
                        dialog.dismiss()
                    })
                }
            }
        }).show(supportFragmentManager)
}

private fun share(
    activity: AppCompatActivity,
    platform: SHARE_MEDIA,
    url: String,
    title: String,
    thumb: UMImage,
    description: String,
    listener: UMShareListener
) {
    val web = UMWeb(url)
    web.title = title//标题
    web.setThumb(thumb)  //缩略图
    web.description = description//描述
    ShareAction(activity)
        .setPlatform(platform)//传入平台
        .withMedia(web)//分享内容
        .setCallback(listener)
        .share()
}